import re

def convert_ranorexpath_to_xpath(ranorex_path):
    '''
    转换RanoreXPath到XPath

    :Args:
     - ranorex_path: RanoreXPath

    :Returns:
        返回Xpath，类型为字符串

    :Example:
     - 例: xpath = convert_ranorexpath_to_xpath("/form[@title='交易前台']/dom[@caption='浜ゆ槗鍓嶅彴']//div[#'app']//section/section/div/div/div/div[2]/div[2]/div[1]/div/div[1]/?/?/form/div[3]/div/div[1]/div[2]/div/ul[2]/li[@innertext='内部转入']")
    '''
    #去掉DOM头
    pattern = "\/form\[@title='.*?']\/dom\[@caption='.*?']"
    ranorex_path = re.sub(pattern, '', ranorex_path)

    #把Ranorex id匹配的方式更改为Xpath
    pattern = "\[#'.*?']"
    rets = re.findall(pattern, ranorex_path)
    for ret in rets:
        ret_id = ret.replace('#', '@id=')
        ranorex_path = ranorex_path.replace(ret, ret_id)

    #去掉ranorex中的?/?
    ranorex_path = ranorex_path.replace('?/?', '')
    print(ranorex_path)

    #把innertext Ranorex方法转换为Xpath方法contains[text(),'文本']
    pattern = "\[@innertext='.*?']"
    rets = re.findall(pattern, ranorex_path)
    print(rets)
    for ret in rets:
        #提取出文字
        word_pattern = "'.*?'"
        word = re.findall(word_pattern, ret)[0]
        print(word)
        ret_text = ret.replace(ret, "[contains(text(),{})]".format(word))
        ranorex_path = ranorex_path.replace(ret, ret_text)

    return ranorex_path


if __name__=="__main__":
    ranorex_path = "/form[@title='交易前台']/dom[@caption='浜ゆ槗鍓嶅彴']//div[#'app']//section/section/div/div/div/div[2]/div[2]/div[1]/div/div[1]/?/?/form/div[3]/div/div[1]/div[2]/div/ul[2]/li[@innertext='内部转入']"
    convert_ranorexpath_to_xpath(ranorex_path)